home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / sprites / bkgnd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-16  |  4.5 KB  |  182 lines

  1. /*
  2.     bkgnd.c
  3.  
  4.     Background image routines
  5. */
  6.  
  7. #include "global.h"
  8.  
  9. //
  10. // Load a new background image.
  11. // All current sprites are deleted.
  12. // The current palette is created from the palette found in the DIB
  13. // The window size is adjusted to fit the new image.
  14. // A new off-screen DIB DC is created to match the size of the bkgnd
  15. // The bkgnd is rendered to the os DIB and the screen repainted
  16. // if requested
  17. //
  18.  
  19. void LoadBackground(LPSTR pszPath, BOOL bUpdateScreen)
  20. {
  21.     RECT rcWnd;
  22.     LPWORD pIndex;
  23.     int i;
  24.  
  25.     dprintf2("LoadBackground(%s)", pszPath ? pszPath : "NULL");
  26.  
  27.     //
  28.     // Delete the current sprite set
  29.     //
  30.  
  31.     DeleteSpriteList();
  32.  
  33.     //
  34.     // Nuke any old DIB
  35.     //
  36.  
  37.     DeleteDIB(pdibBkGnd);
  38.  
  39.     //
  40.     // Try to load the new DIB
  41.     //
  42.  
  43.     pdibBkGnd = LoadDIB(pszPath);
  44.     if (!pdibBkGnd) {
  45.         return;
  46.     }
  47.  
  48.     //
  49.     // Create a new palette based on the background DIB
  50.     //
  51.  
  52.     if (hpalCurrent) DeleteObject(hpalCurrent);
  53.     hpalCurrent = CreateDIBPalette(pdibBkGnd);
  54.     SetSysPalColors(hpalCurrent);
  55.  
  56.     //
  57.     // Adjust the window to fit the new background. But not
  58.     // too teeny weeny.
  59.     //
  60.  
  61.     rcWnd.top = 0;
  62.     rcWnd.left = 0;
  63.     rcWnd.right = max (DIB_WIDTH(pdibBkGnd), 150);
  64.     rcWnd.bottom = max(DIB_HEIGHT(pdibBkGnd), 100);
  65.     AdjustWindowRect(&rcWnd, WS_OVERLAPPEDWINDOW, TRUE);
  66.     SetWindowPos(hwndMain,
  67.                  NULL,
  68.                  0,
  69.                  0,
  70.                  rcWnd.right-rcWnd.left,
  71.                  rcWnd.bottom-rcWnd.top,
  72.                  SWP_NOACTIVATE | SWP_NOMOVE);
  73.  
  74.     //
  75.     // Delete any existing off-screen DC and its DIB
  76.     //
  77.  
  78.     if (hdcOffScreen) {
  79.         DeleteDC(hdcOffScreen);
  80.         hdcOffScreen = NULL;
  81.     }
  82.  
  83.     DeleteDIB(pdibOffScreen);
  84.  
  85.     //
  86.     // Create a new off-screen DC using the DIB driver which 
  87.     // has the same size as the background DIB and the same color
  88.     // organization.  The data is in CF_DIB format with the bits
  89.     // immediately following the header.
  90.     //
  91.  
  92.     pdibOffScreen = CreateCompatibleDIB(pdibBkGnd);
  93.     if (!pdibOffScreen) {
  94.         dprintf1("Failed to create off-screen DIB");
  95.         return;
  96.     }
  97.  
  98.     //
  99.     // Create a DIB driver DC
  100.     //
  101.  
  102.     hdcOffScreen = CreateDC("DIB", NULL, NULL, (LPSTR)pdibOffScreen);
  103.     if (!hdcOffScreen) {
  104.         dprintf1("Failed to create off-screen DC");
  105.     } else {
  106.         dprintf3("Off-screen DC created");
  107.     }
  108.  
  109. #ifdef DEBUG
  110.  
  111.     //
  112.     // Show what the DIB device driver supports
  113.     //
  114.  
  115.     {
  116.  
  117.         WORD wCaps;
  118.  
  119.         wCaps = GetDeviceCaps(hdcOffScreen, RASTERCAPS);
  120.         if (wCaps & RC_DI_BITMAP) {
  121.             dprintf3("DIB driver DC supports GetDIBits and SetDIBits");
  122.         } else {
  123.             dprintf3("DIB driver DC doesn't support GetDIBits or SetDIBits");
  124.         }
  125.         if (wCaps & RC_DIBTODEV) {
  126.             dprintf3("DIB driver DC supports SetDIBitsToDevice");
  127.         } else {
  128.             dprintf3("DIB driver DC doesn't support SetDIBitsToDevice");
  129.         }
  130.         if (wCaps & RC_STRETCHDIB) {
  131.             dprintf3("DIB driver DC supports StretchDIBits");
  132.         } else {
  133.             dprintf3("DIB driver DC doesn't support StretchDIBits");
  134.         }
  135.         wCaps = GetDeviceCaps(hdcOffScreen, CAPS1);
  136.         if (wCaps & C1_TRANSPARENT) {
  137.             dprintf3("DIB driver DC supports transparency");
  138.         } else {
  139.             dprintf3("DIB driver DC doesn't support transparency");
  140.         }
  141.     }
  142.  
  143. #endif // DEBUG
  144.  
  145.     //
  146.     // Create a new 1:1 lookup table so we can use DIB_PAL_COLORS
  147.     // when we call StretchDIBits to blt data from the off-screen
  148.     // DC to the window DC.
  149.     //
  150.  
  151.     if (!pPalClrTable) {
  152.         pPalClrTable = (LPBITMAPINFO) ALLOCATE(sizeof(BITMAPINFOHEADER)
  153.                                                + 256 * sizeof(WORD));
  154.         if (!pPalClrTable) {
  155.             dprintf1("Out of memory for 1:1 clr table");
  156.             return;
  157.         }
  158.     }
  159.  
  160.     //
  161.     // Set up the table to match the off-screen DIB
  162.     // by copying the BITMAPINFOHEADER and building a 1:1 color table
  163.     //
  164.  
  165.     _fmemcpy(pPalClrTable,
  166.              pdibOffScreen,
  167.              sizeof(BITMAPINFOHEADER));
  168.  
  169.     pIndex = (LPWORD)((LPSTR)pPalClrTable + sizeof(BITMAPINFOHEADER));
  170.     for (i=0; i<256; i++) {
  171.         *pIndex++ = (WORD) i;
  172.     }
  173.  
  174.     //
  175.     // Draw the background to the off-screen DIB
  176.     // and update the screen if required
  177.     //
  178.  
  179.     Redraw(NULL, bUpdateScreen);
  180.  
  181. }
  182.